Repository navigation
% of two integers of any width is an integer remainder - #852
Merged
Merged
Conversation
Only a 64-bit operand made `%` an integer remainder (#571); two narrower integers were cast to number. A hash modulo the length of a table - every lookup of a Map or a Set - was an fmod call and two conversions, and modulo a length of 0 it was NaN made into an index. It is one urem/srem now. The result has the integer type, where it was a number. The type: an integer literal that fits takes the other operand's; otherwise the wider one, and of two of a width the left one. A signed and an unsigned one that an s64 holds both of are done in s64, where neither changes its value. 64-bit operands are as they were. Breaking: - the result of `int % int` is an integer, not a number. own_interface_view.ts put it in an object literal's field seen through an interface that declares number, and now declares it `: number`. - a zero divisor is undefined, as integer division by zero is in C (a fault on x86), where it was NaN; so is the least value of a signed type `% -1`. A number on either side is still a number's remainder. 00int_remainder.ts covers s8 to u32, literals on either side, mixed signedness, mixed widths, `%=`, what `>>>` gives, and the number path. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
%of two integers of any width is now an integer remainder with an integer result. Since #571 that was true only when one operand was 64-bit; two narrower integers were cast to number.Why
Found in the default library:
MapandSetcompute<uint>hashCode % <uint>buckets.lengthon every lookup. That compiled to twouitofp, anfrem(anfmodcall) and anfptosi. Modulo a length of 0 it was NaN converted to an index, which is undefined (ASDAlexander77/TypeScriptCompilerDefaultLib#100 removed the zero length). It is now:The type of the result
u % 7is au32).s64holds both of are done ins64, so neither changes its value (s32 % u32,s16 % u16).Breaking
int % intis an integer, not a number.own/own_interface_view.tsputi % 10into an object literal's field seen through an interface declaringnumber, which no longer compiled; it now declares the constant: number. That was the only test affected.% -1. This is what TSLANG_ISSUES 16: % of 64-bit integers is an integer remainder #571 already did for 64-bit operands.Change
tslang/lib/TypeScript/MLIRGenImpl.h: the%case of the cast step inadjustTypesForBinaryOp. The lowering is unchanged: it already picksremsi/remuiby type.tslang/test/tester/tests/00int_remainder.ts(new, registered for compile and JIT):s8tou32, literals on either side, mixed signedness, mixed widths,%=, what>>>gives, and the number path.00int64_remainder.ts: one comment that said narrower integers keep the number path.Testing
Map.getover 200,000 keys went from about 18 ms to about 17 ms. That is small and close to noise.🤖 Generated with Claude Code